package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Permission;
import com.itheima.health.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.itheima.health.pojo.Menu;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/permission")
@Slf4j
public class PermissionController {
        @Reference
        private PermissionService permissionService;

        /**
         * 新增
         * 1、rpc调用完成新增业务
         * 2、返回结果
         *
         * @param permission
         * @return
         */
        @PostMapping("/add")
        @PreAuthorize("hasAuthority('CHECKITEM_ADD')")
        public Result add(@RequestBody Permission permission){
            log.info("[权限-新增]data:{}",permission);
            permissionService.add(permission);
            return new Result(true, "新增权限成功");
        }

        /**
         * 分页查询
         *
         * @param queryPageBean
         * @return
         */
        @GetMapping("findPage")
        @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
        public Result findPage(QueryPageBean queryPageBean) {
            log.info("[权限-分页查询]data:{}", queryPageBean);
            //rpc查询数据
            PageResult pageResult = permissionService.pageQuery(queryPageBean);
            //封装返回结果
            return new Result(true,"权限分页查询成功", pageResult);
        }

        /**
         * 根据id删除
         *
         * @param id
         * @return
         */
        @RequestMapping("/delete")
        @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
        public Result delete(Integer id){
            log.info("[权限-根据id删除]id：{}",id);
            //RPC调用处理业务
            permissionService.delete(id);
            return new Result(true,"权限删除成功");
        }

        /**
         * 编辑
         *
         * @param
         * @return
         */
        @RequestMapping("/edit")
        @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")
        public Result edit(@RequestBody Permission permission) {
            log.info("[权限-编辑]data:", permission);
            permissionService.edit(permission);
            return new Result(true, "权限编辑成功");
        }

        /**
         * 根据Id查询
         *
         * @param id
         * @return
         */
        @RequestMapping("/findById")
        @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
        public Result findById(Integer id) {
            log.info("[权限-根据ID查询]id:{}", id);
            Permission permission= permissionService.findById(id);
            return new Result(true,"权限根据id查询成功",permission);
        }

        /**
         * 查询所有权限
         * @return
         */
        @RequestMapping("/findAll")
        @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
        public Result findAll(){
            log.info("[权限-查询所有]~");
            List<Permission> permissions = permissionService.findAll();
            return new Result(true,"查询所有权限成功",permissions);
        }


    @RequestMapping("/findPermissionIdsByRoleid")
    public Result findPermissionIdsByRoleid(Integer id){
        log.info("[角色-编辑-根据角色id查权限]id:{}",id);
        List<Integer> permissionIds =permissionService.findPermissionIdsByRoleid(id);
        return new Result(true,MessageConst.QUERY_PERMISSION_SUCCESS,permissionIds);
    }
}
